function init(){
	submitUpdateForm('#panel-new-packet .btn-submit-update' ,evtAfterInsertPacket);
	submitUpdateForm('#panel-register-packet .btn-submit-update' ,evtAfterRegisterPacket);
	showPanelUpdate('#tb-packet', getUpdatePacket);
	showPanelUpdate('#tb-register-packet', getRegisterPacket);
	updatePacket();
}

function evtAfterInsertPacket(){
	var row = $('#tb-packet thead .tr-temp2').clone().removeClass('hidden').removeClass('tr-temp2');
	var index = $('#tb-packet tbody tr').length + 1;
	row.find('.index').html(index);
	row.find('.input-packet-title').html($('#packet-title').val());
	row.find('.input-packet-product-count').html($('#packet-product-count').val());
	row.find('.input-packet-price').html($('#packet-price').val());
	row.find('.input-packet-delete-flg').html(function(){
		var checkbox = $(this).find('input.checkbox');
		var id = checkbox.attr('data-id') + index;
		checkbox.attr('id', id).removeAttr('data-id');
		$(this).find('label').attr('for', id);
	});
	$('#tb-packet tbody').append(row);
	resetForm('#panel-new-packet');
}

function evtAfterRegisterPacket(data){
	var row = $('#tb-register-packet thead .tr-temp2').clone().removeClass('hidden').removeClass('tr-temp2');
    var index = $('#tb-register-packet tbody tr').length + 1;
	row.find('.index').html(index);
    row.find('.input-packet-shop').attr('data-shop-id', $('#packet-shop').val());
	row.find('.input-packet-packet').attr('data-packet-id', $('#packet-packet').val());
	row.find('.input-packet-shop').html($('#packet-shop option:selected').text());
	row.find('.input-packet-packet').html($('#packet-packet option:selected').text());
	row.find('.input-packet-created-at').html(data.created_at);
	row.find('.input-packet-delete-flg').html(function(){
		var checkbox = $(this).find('input.checkbox');
		var id = checkbox.attr('data-id') + index;
		checkbox.attr('id', id).removeAttr('data-id');
		$(this).find('label').attr('for', id);
	});
    $('#tb-register-packet tbody').append(row);
	resetForm('#panel-register-packet');
}

function getUpdatePacket(row){
	var html = row.parents('table').find('tr.tr-temp').clone().removeClass('hidden').removeClass('tr-temp').addClass('tr-update-temp');
	html.find('.input-packet-title input').val(row.find('td.input-packet-title').html().trim());
	html.find('.input-packet-product-count input').val(row.find('td.input-packet-product-count').html().trim());
	html.find('.input-packet-price input').val(row.find('td.input-packet-price').attr('data-price').trim());
	html.find('th.index').html(row.find('th.index').html().trim());
	html.find('.input-packet-delete-flg .input-packet-id').val(row.attr('data-id'));
	return html;
}

function getRegisterPacket(row){
	var html = row.parents('table').find('tr.tr-temp').clone().removeClass('hidden').removeClass('tr-temp').addClass('tr-update-temp');
	html.find('.input-packet-shop select#t-packet-shop').val(row.find('td.input-packet-shop').attr('data-shop-id').trim());
	html.find('.input-packet-packet select#t-packet-packet').val(row.find('td.input-packet-packet').attr('data-packet-id').trim());
	html.find('th.index').html(row.find('th.index').html().trim());
	html.find('.input-packet-active-flg .input-packet-id').val(row.attr('data-id'));
	return html;
}

function updatePacket(){
	$(document.body).on('click', '#tb-packet .btn-inline-update', function(){
		var aInput = $(this).closest('tr').find('input[type=text], input[type=hidden]');
		var packet = new Object();
		aInput.each(function(i,j){
			var value = $(this).val();
			var name = $(this).attr('name');
			packet[name] = value;
		});
		var url = $(this).parents('table').attr('data-href');
		changeState(url, {packet: packet}, evtAfterUpdatePacket);
	});

	$(document.body).on('click', '#tb-register-packet .btn-inline-update', function(){
		var aInput = $(this).closest('tr').find('select, input[type=hidden]');
		var packet = new Object();
		aInput.each(function(i,j){
			var value = $(this).val();
			var name = $(this).attr('data-name');
			packet[name] = value;
		});
		var url = $(this).parents('table').attr('data-href');
		changeState(url, {packet: packet}, evtAfterUpdateRegisterPacket);
	});

	$('.chk-packet-flg').change(function(){
		var url = $(this).parents('table').attr('data-href');
		var value = $(this).is(':checked')==true?1:0;
		var name = $(this).attr('data-name');
		var packet = {id: $(this).parents('tr').attr('data-id')};
		packet[name] = value;
		changeState(url, {packet: packet});
	});

	$('.chk-register-packet-flg').change(function(){
		var url = $(this).parents('table').attr('data-accept');
		var value = $(this).is(':checked')==true?1:0;
		var name = $(this).attr('data-name');
		var packet = {id: $(this).parents('tr').attr('data-id')};
		packet[name] = value;
		changeState(url, {packet: packet});
	});
}

function evtAfterUpdatePacket(data){
	if(data.code==1){
	   	var row = $('tr.editting');
		var tempRow = $('tr.tr-update-temp')
		row.find('.input-packet-title').html(tempRow.find('.input-packet-title input').val());
		row.find('.input-packet-product-count').html(tempRow.find('.input-packet-product-count input').val());
        row.find('.input-packet-price').html(formatCurrency(tempRow.find('.input-packet-price input').val()));
		row.find('.input-packet-price').attr('data-price', tempRow.find('.input-packet-price input').val());
		cancelUpdate();
	}
}

function evtAfterUpdateRegisterPacket(data){
	if(data.code==1){
		var row = $('tr.editting');
		var tempRow = $('tr.tr-update-temp')
		row.find('.input-packet-shop').html(tempRow.find('.input-packet-shop select#t-packet-shop option:selected').text());
		row.find('.input-packet-packet').html(tempRow.find('.input-packet-packet select#t-packet-packet option:selected').text());
		row.find('.input-packet-shop').attr('data-shop-id', tempRow.find('.input-packet-shop select#t-packet-shop').val());
		row.find('.input-packet-packet').attr('data-packet-id', tempRow.find('.input-packet-packet select#t-packet-packet').val());
		cancelUpdate();
	}
}